package com.zzsy.carosshopprojects.activitys.home.mainpage.common.payresult;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.OrderDetialBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.FormatTimeUtil;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import java.math.BigDecimal;

import androidx.lifecycle.MutableLiveData;

public class PayResultMode extends BaseViewMode {
    private MutableLiveData<OrderDetialBean> liveData;

    public MutableLiveData<OrderDetialBean> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    /**
     * 获取订单详情接口
     *
     * @param context
     * @param orderNumber
     */
    public void getOrderDetail(Context context, String orderNumber) {
        RequestUtils.OrderDetail(context, orderNumber, new MyObserver<OrderDetialBean>(context) {
            @Override
            public void onSuccess(OrderDetialBean result) {
                liveData.postValue(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context, errorMsg);
            }
        });


    }

    /**
     * 查询是否可以打印小票
     *
     * @param context
     * @param bean
     */
    public void IsPrintTicket(Context context, OrderDetialBean bean, int kind) {
        RequestUtils.UpdatePrintStatus(context, bean.getOrderNumber(), new MyObserver<BaseBean>(context, false) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    switch (kind) {
                        case 0://操作员收费打印小票
                            PrintHelper.getInstance().PrintOperatorChargeTicket(bean, false);
                            break;
                        case 1://操作员会员消费打印小票
                            printMemberPay(bean,false);
                            break;

                    }

                } else {
                    ToastUtils.showToast(context, errorMsg);
                }
            }
        });
    }

    /**
     * 打印会员消费小票
     */
    public void printMemberPay(OrderDetialBean bean,boolean isReprint) {
        String orderNumber = bean.getOrderNumber();
        int staffId = bean.getStaffId();
        String orderCreateTime = String.valueOf(bean.getOrderCreateTime());
        String time = "";
        if (!StringUtils.isEmpty(orderCreateTime)) {
            time = FormatTimeUtil.formatTimeStamp2DateStr(String.valueOf(Long.parseLong(orderCreateTime) / 1000), "yyyy-MM-dd HH:mm:ss");
        }
        int orderAmount = bean.getOrderAmount();
        int payAmounts = bean.getPayAmount();
        String oilNum = bean.getOilNum();
        String storeName = bean.getStoreName();
        //获取会员余额
        String memberBlanceAmounts = bean.getRechargeBalance();
        if (StringUtils.isEmpty(memberBlanceAmounts)) {
            memberBlanceAmounts = "0";
        }
        String payTypeStr = "";
        String payType = bean.getPayType();
        if ("1".equals(payType)) {
            payTypeStr = "支付宝支付";
        } else if ("2".equals(payType)) {
            payTypeStr = "微信支付";
        } else if ("3".equals(payType)) {
            payTypeStr = "银联支付";
        } else if ("4".equals(payType)) {
            payTypeStr = "余额支付";
        }

        /**
         * 获取优惠券个数
         * */
        int size = 0;
        if (bean.getCouponDTOs() != null) {
            size = bean.getCouponDTOs().size();
        }
        double oilsPrices = 0;
        //获取加油升数
        try {
            String oilPrice = bean.getOilPrice();
            if (!StringUtils.isEmpty(oilPrice)) {
                BigDecimal bigDecimalAmount = ConverterHelper.toBigDecimal(oilPrice).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                oilsPrices = bigDecimalAmount.doubleValue();
            } else {
                oilsPrices = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String riseNew = "";
        try {
            riseNew = bean.getRise();

            if (StringUtils.isEmpty(riseNew)) {
                riseNew = "0";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String oilPriceSwitch;
        oilPriceSwitch = bean.getOilPriceSwitch();
        if (StringUtils.isEmpty(oilPriceSwitch)) {
            oilPriceSwitch = "1";
        }

        /**
         * 红包
         * */
        String memberRedPackedAmount = bean.getMemberRedPackedAmount();
        double memberRedPackedAmountDou;
        if (!StringUtils.isEmpty(memberRedPackedAmount)) {
            memberRedPackedAmountDou = ConverterHelper.toBigDecimal(memberRedPackedAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {
            memberRedPackedAmountDou = 0;
        }

        String memberBlanceAmountsDou = ConverterHelper.toBigDecimal(memberBlanceAmounts).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        String orderAmountDou = ConverterHelper.toBigDecimal(orderAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        String shiAmount = ConverterHelper.toBigDecimal(payAmounts).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        String activityReductionDou = ConverterHelper.toBigDecimal(orderAmountDou).subtract(ConverterHelper.toBigDecimal(shiAmount)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        String Phone =bean.getPhone();
        PrintHelper.getInstance().PrintMemberOrder(orderNumber, time, orderAmountDou, activityReductionDou, shiAmount, oilNum, storeName, payTypeStr, memberBlanceAmountsDou, payType, size + "", riseNew, oilsPrices + "", oilPriceSwitch, memberRedPackedAmountDou + "", isReprint,bean.getMemberInfoCardDTO(),bean.getOilNum(),Phone,bean.getRedPackedAmount(),bean.getStoreDownAmount());
    }

}
